package com.example.demo.web.controller;

import com.example.demo.bean.Account;
import com.example.demo.servive.IAccountService;
import com.example.demo.utils.Message;
import com.example.demo.utils.MessageUtil;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private IAccountService accountService;
    @GetMapping("findAll")
    public Message findAll(){
        List<Account>list = accountService.findAll();
        return MessageUtil.success(list);
    }
    @PostMapping("findById")
    public Message findById(long id){
        Account account = accountService.findById(id);
        return MessageUtil.success(account);
    }
    @DeleteMapping("deleteById")
    public Message deleteById(long id){
        accountService.deleteById(id);
        return MessageUtil.success("删除成功");
    }
    @PostMapping("save")
    public Message save(@NotNull String account,@NotNull String passward){
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Account a = new Account();
        a.setLoginIp(req.getRemoteAddr());
        a.setAccount(account);
        a.setAccount(passward);
        accountService.save(a);
        return MessageUtil.success("保存成功");
    }

}
